package com.yzh.wanandroid.base

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

abstract class MvvmFragment<V : ViewDataBinding, VM : BaseFragmentViewModel> : BaseFragment<V>() {

    private val TAG = "MvvmFragment"

    abstract fun initViewModel(): VM

    lateinit var mViewModel: VM

    private var isFirstLoad = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = initViewModel()
        lifecycle.addObserver(mViewModel)
    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            onLazyLoad()
            isFirstLoad = false
        }
    }

    inline fun <reified VM : ViewModel> createViewModel(): VM {
        return  activityViewModels<VM>{ViewModelProvider.AndroidViewModelFactory(requireActivity().application)}.value
//        return ViewModelProvider(fragment).get(clazz)
    }

    open fun onLazyLoad() {}
}